package org.csu.mall.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Bank;
import org.csu.mall.persistence.BankMapper;
import org.csu.mall.service.IBankService;
import org.csu.mall.utils.MD5Util;
import org.csu.mall.utils.UsernameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BankServiceImpl implements IBankService {
    @Autowired
    private BankMapper bankMapper;

    @Override
    public UniformResponse<String> checkCartNo(String id){
        String username= UsernameUtil.getUsername("username");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            int rows = bankMapper.selectCount(Wrappers.<Bank>query().eq("id",id));
            if (rows > 0 ){
                return UniformResponse.createForErrorMessage("银行卡已被绑定");
            }else {
                return UniformResponse.createForSuccessMessage("银行卡可用");
            }
        }
    }
    @Override
    public UniformResponse<Bank> addBank(Bank bank) {
        String username= UsernameUtil.getUsername("username");
        Integer userid= Integer.valueOf(UsernameUtil.getUserId("userid"));
        if(username==null){
            return UniformResponse.notLogining();
        }else{
            UniformResponse<String> checkCartNo = this.checkCartNo(bank.getCartNo());
            if(checkCartNo.isSuccess()){
                bank.setUserId(userid);
                bank.setPaymentPassword(MD5Util.md5Encrypt32Upper(bank.getPaymentPassword()));
                bank.setCreateTime(LocalDateTime.now());
                bank.setUpdateTime(LocalDateTime.now());
                int rows = bankMapper.insert(bank);
                bank.setPaymentPassword(StringUtils.EMPTY);
                if(rows == 0){
                    return UniformResponse.createForErrorMessage("绑定银行卡失败");
                }
                return UniformResponse.createForSuccess("绑定银行卡成功",bank);
            }else {
                return UniformResponse.createForErrorMessage("银行卡已被绑定");
            }
        }
    }

    @Override
    public UniformResponse<String> deleteBank(String id) {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            Map<String, Object> columnMap = new HashMap<String, Object>();
            columnMap.put("user_id", userid);
            columnMap.put("id", id);
            int rows=bankMapper.deleteByMap(columnMap);
            if(rows==0){
                return UniformResponse.createForSuccessMessage("银行卡删除失败");
            }
            return UniformResponse.createForSuccessMessage("银行卡删除成功");
        }
    }
    @Override
    public UniformResponse<String> checkBankPassword(String id,String paymentPassword) {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            String md5paymentPassword=MD5Util.md5Encrypt32Upper(paymentPassword);
            Bank bank = bankMapper.selectOne(
                    Wrappers.<Bank>query().eq("user_id",userid).eq("id",id));
            if(bank == null){
                return UniformResponse.createForErrorMessage("无该银行卡号");
            }
            if(bank.getPaymentPassword().equals(md5paymentPassword)){
                return UniformResponse.createForSuccessMessage("支付成功");
            }
            return UniformResponse.createForErrorMessage("支付失败");
        }
    }

    @Override
    public UniformResponse<Bank> getBankInfoOne(String id) {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            Bank bank = bankMapper.selectOne(
                    Wrappers.<Bank>query().eq("user_id",userid).eq("id",id));
            if(bank == null){
                return UniformResponse.createForErrorMessage("银行卡不存在");
            }
            bank.setPaymentPassword(StringUtils.EMPTY);
            return UniformResponse.createForSuccess("银行卡信息查询成功",bank);
        }
    }

    @Override
    public UniformResponse<List<Bank>> getBankInfo() {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            List<Bank> bankList= bankMapper.selectList(Wrappers.<Bank>query().eq("user_id",userid));
            if(bankList==null){
                return UniformResponse.createForErrorMessage("用户未绑定银行卡") ;
            }
            for(int i=0;i<bankList.size();i++){
                bankList.get(i).setPaymentPassword(StringUtils.EMPTY);
            }
            return UniformResponse.createForSuccess("查询成功",bankList);

        }
    }

    @Override
    public UniformResponse<Bank> updateBank(Integer id, String phone) {
        String username= UsernameUtil.getUsername("username");
        String userid= UsernameUtil.getUserId("userid");
        if(username==null){
            return UniformResponse.notLogining();
        }else {
            Bank bank= bankMapper.selectById(id);
            if(bank==null){
                return UniformResponse.createForErrorMessage("该银行卡被删除或不存在") ;
            }
            bank.setPhone(phone);
            bank.setUpdateTime(LocalDateTime.now());
            bankMapper.updateById(bank);
            return UniformResponse.createForSuccess("查询成功",bank);

        }
    }
}
